package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.clients.GoodsClient;
import com.leyou.page.clients.SpecClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    public Map<String, Object> loadData(Long spuId){

        Map<String,Object> dataMap = new HashMap<>();

        //根据spuId要查询出spu

        Spu spu = goodsClient.querySpuById(spuId);

        dataMap.put("spu",spu);

        //根据spuId要查询出spuDetail

        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);

        dataMap.put("spuDetail",spuDetail);

        List<Sku> skus = this.goodsClient.querySkuBySpuId(spuId);

        dataMap.put("skus",skus);
        //根据spu分类id要查询出规格信息
        //根据spu分类id要查询出分类对应的规格参数的值信息
        //根据spuId查询sku
        //根据spu的信息查询对应的规格参数组，以及规格参数组内的信息


        // 查询商品分类下的特有规格参数
        List<SpecParam> params =
                this.specClient.querySpecParam(null, spu.getCid3(), null, false);

        // 处理成id:name格式的键值对
        Map<Long,String> paramMap = new HashMap<>();
        for (SpecParam param : params) {
            paramMap.put(param.getId(), param.getName());//4:机身颜色，5，内存
        }
        dataMap.put("paramMap", paramMap);


        List<SpecGroup> specGroups = this.specClient.querySpecGroups(spu.getCid3());

        dataMap.put("specGroups",specGroups);

        return dataMap;
    }
}
